package org.devops

import java.time.LocalDateTime
import java.time.format.DateTimeFormatter

def printName(name){
    println(name)
}

def exeSshPublisher(configName, execCommand, sourceFiles, remoteDirectory, flatten) {
    sshPublisher(publishers: [sshPublisherDesc(configName: configName, transfers: [sshTransfer(cleanRemote: false, excludes: '', execCommand: execCommand, execTimeout: 1200000, flatten: flatten, makeEmptyDirs: false, noDefaultExcludes: false, patternSeparator: '[, ]+', remoteDirectory: remoteDirectory, remoteDirectorySDF: false, removePrefix: '', sourceFiles: sourceFiles)], usePromotionTimestamp: false, useWorkspaceInPromotion: false, verbose: true)])
}

def remoteExec(configName, execCommand) {
    exeSshPublisher(configName, execCommand, '', '', false)
}


def processIpMap(String[] IPS ) {
    def internal_ansible = []
    def internet_ansible = []
    def kj_internet_ansible = []
    def sx_internet_ansible = []
    def sx2_internet_ansible = []

    Map ipMap = [:]
    for (String ip in IPS) {
        if (ip.startsWith("172.30.30")) {
            kj_internet_ansible.add(ip)
        } else if (ip.startsWith("172.30.31")) {
            sx_internet_ansible.add(ip)
        } else if (ip.startsWith("172.30.37")) {
            sx2_internet_ansible.add(ip)
        } else if (ip.startsWith("172.25")) {
            internal_ansible.add(ip)
        } else if (ip.startsWith("172.30")) {
            internet_ansible.add(ip)
        }
    }

    if (internal_ansible.size() > 0) {
        ipMap.put("internal_ansible", internal_ansible)
    }

    if (sx_internet_ansible.size() > 0) {
        ipMap.put("sx_internet_ansible", sx_internet_ansible)
    }

    if (sx2_internet_ansible.size() > 0) {
        ipMap.put("sx2_internet_ansible", sx2_internet_ansible)
    }

    if (kj_internet_ansible.size() > 0) {
        ipMap.put("kj_internet_ansible", kj_internet_ansible)
    }

    if (internet_ansible.size() > 0) {
        ipMap.put("internet_ansible", internet_ansible)
    }

    return ipMap
}

def formatTime(LocalDateTime dateTime, String pattern) {
    DateTimeFormatter formatter = DateTimeFormatter.ofPattern(pattern)
    return dateTime.format(formatter)
}

def getNowTime() {
    return formatTime(LocalDateTime.now(), "yyyy-MM-dd HH:mm:ss")
}
